package com.ztf.shopping.service.imp;

import com.ztf.shopping.bean.Article;
import com.ztf.shopping.bean.Order;
import com.ztf.shopping.bean.User;
import com.ztf.shopping.dao.UserDao;
import com.ztf.shopping.dao.imp.UserDaoImp;
import com.ztf.shopping.service.UserService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class UserServiceImp implements UserService {
    private final UserDao userDao = new UserDaoImp();
    @Override
    public String registeredFindName(String name) {
        return userDao.findName(name);
    }

    @Override
    public User loginUser(User user) {
        return userDao.login(user);
    }

    @Override
    public boolean registerServlet(User regUser) {

        return userDao.registerServlet(regUser);
    }

    @Override
    public List<Order> findAllShoppingCart(String name) {
        return userDao.findShoppingCart(name);
    }

    @Override
    public void sureRegister(String name) {
        userDao.sureRegister(1,name);
    }

    @Override
    public Map<String, String> addShopCount(Object user, String id, String count) {
        //在服务层处理逻辑和业务

        User user1 = (User) user;
        int articleId = Integer.parseInt(id);
        Map<String,String> map = new HashMap<>();
        //首先去数据库查询该商品
        Article art = userDao.findArtilceCount(articleId);
        int newCount = Integer.parseInt(count);
        String  newMoney;
        ++newCount;
        newMoney = art.getPrice().multiply(new BigDecimal(newCount+"")).toString();
        //增加用户在购物车里的数据
        userDao.changeCountById(user1.getName(),art.getName(),newCount,newMoney);
        map.put("newCount",newCount+"");
        map.put("money",newMoney);
        return map;
    }

    @Override
    public Map<String, String> lessShopCount(Object user, String id, String count) {
        User user1 = (User) user;
        int articleId = Integer.parseInt(id);
        Map<String,String> map = new HashMap<>();
        //首先去数据库查询该商品
        Article art = userDao.findArtilceCount(articleId);
        int newCount = Integer.parseInt(count);
        String newMoney;
        if (newCount<=1){
            newMoney = art.getPrice().multiply(new BigDecimal(newCount+"")).toString();
            map.put("newCount",newCount+"");
            map.put("money",newMoney);
            return map;
        }
        --newCount;
        newMoney = art.getPrice().multiply(new BigDecimal(newCount+"")).toString();
        //增加用户在购物车里的数据
        userDao.changeCountById(user1.getName(),art.getName(),newCount,newMoney);

        map.put("newCount",newCount+"");
        map.put("money",newMoney);
        return map;


    }

    @Override
    public void delOrder(Object loginUser, String id) {
        //实现逻辑  删除购物车里的某条数据
        User user = (User) loginUser;
        userDao.delOrder(user.getName(),id);
    }

    @Override
    public void addOrder(Object loginUser, String id) {
        //添加订单

        /*
        1.首先先去查看当前用户是否有此订单未支付
        2.,如果未支付,则增加当前订单的数量
        3.如果没有未支付的此订单,则新创建一个新的订单;
         */
        User user = (User) loginUser;
        int articleId = Integer.parseInt(id);
        Article art = userDao.findArtilceCount(articleId); //查询该商品

        Order o = userDao.findShoppingCartByArticleId(user.getName(),articleId);
        System.out.println(o);
        if (o==null){
            //说明没有这样的订单
            //增加一个这样的订单
            Order order = new Order();
            order.setArticleId(articleId);
            order.setUserName(user.getName());
            order.setArticleName(art.getName());
            order.setMoney(art.getPrice().toString());
            order.setNumber(1);
            userDao.addOrder(order);

        }else {
            //说明存在这样的一个订单
            //增加该订单的数量
            int i = o.getNumber()+1;
            userDao.changeCountById(user.getName(),art.getName(),i,art.getPrice().multiply(new BigDecimal(i+"")).toString());
        }
    }

    @Override
    public void payOrder(Object loginUser, String[] split) {
        User user = (User) loginUser;
        Order order = null;
        List<Order> list = new ArrayList<>();
        for (String s : split) {
            order = new Order();
            order.setArticleId(Integer.parseInt(s));
            list.add(order);
        }

        userDao.payOrder(user.getName(),list);
    }

    @Override
    public void changeUser(User loginUser) {
        userDao.changeUser(loginUser);
    }
}
